﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using Core.DataAccess.Model.Project.Queue;
using Core.Framework.Model.ViewModel;
using Core.ApiClient.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Core.ApiClient.Controllers.Socket
{
    /// <summary>
    /// 群组操作
    /// </summary>
    [Route(".v1/socket/[controller]")]
    [ApiController]
    public class GroupController : BaseApiController
    {
        /// <summary>
        /// 根据群组ID获取群组信息
        /// </summary>
        /// <param name="id">群组关键字</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public IResult Get(int id)
        {
            var result = this.iSocketGroup.GetById(id, iClientInfo.ProjectToken);

            return new ApiResult
            {
                code = result.Item2 ? CodeResult.SUCCESS : CodeResult.DATA_NOT_FOUND,
                date = result.Item1
            };
        }

        /// <summary>
        /// 查询用户群组信息列表
        /// </summary>
        /// <returns></returns>
        [HttpGet(".grouplist")]
        [Description("查询用户群组信息列表")]
        public IResult Get()
        {
            var result = this.iSocketGroup.GetByUserKey(iClientInfo.Key.ToString(), iClientInfo.ProjectToken);

            return new ApiResult
            {
                code = result.Item2 ? CodeResult.SUCCESS : CodeResult.DATA_NOT_FOUND,
                date = result.Item1
            };
        }

        /// <summary>
        /// 查询用户以加入群组集
        /// </summary>
        /// <returns></returns>
        [HttpGet(".groups")]
        [Description("查询用户以加入群组集")]
        public IResult GetGroupsByUserKey()
        {
            var result = this.iSocketGroup.GetGroupsByUserKey(iClientInfo.Key.ToString(), iClientInfo.ProjectToken);

            return new ApiResult
            {
                code = result.Item2 ? CodeResult.SUCCESS : CodeResult.DATA_NOT_FOUND,
                date = result.Item1
            };
        }

        /// <summary>
        /// 查询群组成员
        /// </summary>
        /// <param name="id">群组关键字</param>
        /// <returns></returns>
        [HttpGet(".groupuser{id}")]
        [Description("查询群组成员")]
        public IResult GetGroupUsersByGroupKey(int id)
        {
            var result = this.iSocketGroup.GetGroupUsersByGroupKey(id, iClientInfo.ProjectToken);

            return new ApiResult
            {
                code = result.Item2 ? CodeResult.SUCCESS : CodeResult.DATA_NOT_FOUND,
                date = result.Item1
            };
        }

        /// <summary>
        /// 创建群组
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IResult Create(ProjectModuleGroup model)
        {
            var result = this.iSocketGroup.Create(model);

            return new ApiResult
            {
                code = result.Item3 ? CodeResult.SUCCESS : CodeResult.PARAMS_IS_INVALID,
                date = result.Item1,
                info = result.Item2
            };
        }

        /// <summary>
        /// 加入群组
        /// </summary>
        /// <returns></returns>
        [HttpPost(".add")]
        [Description("加入群组")]
        public IResult AddGroupUser(ProjectModuleGroupUser model)
        {
            var result = this.iSocketGroup.AddGroupUser(model);

            return new ApiResult
            {
                code = result.Item3 ? CodeResult.SUCCESS : CodeResult.PARAMS_IS_INVALID,
                date = result.Item1,
                info = result.Item2
            };
        }


        /// <summary>
        /// 修改群组信息
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        public IResult Update(ProjectModuleGroup model)
        {
            var result = this.iSocketGroup.Update(model);

            return new ApiResult
            {
                code = result.Item3 ? CodeResult.SUCCESS : CodeResult.PARAMS_IS_INVALID,
                date = result.Item1,
                info = result.Item2
            };
        }

        /// <summary>
        /// 修改组员信息
        /// </summary>
        /// <returns></returns>
        [HttpPut(".update")]
        [Description("修改组员信息")]
        public IResult UpdateGroupUser(ProjectModuleGroupUser model)
        {
            var result = this.iSocketGroup.UpdateGroupUser(model);

            return new ApiResult
            {
                code = result.Item3 ? CodeResult.SUCCESS : CodeResult.PARAMS_IS_INVALID,
                date = result.Item1,
                info = result.Item2
            };
        }


        /// <summary>
        /// 解散群组
        /// </summary>
        /// <returns></returns>
        [HttpDelete("{id}")]
        [Description("解散群组")]
        public IResult Delete(ScoketGroupRequest request)
        {
            var result = this.iSocketGroup.Delete(request.id, request.userKey, request.projectToken);

            return new ApiResult
            {
                code = result.Item2 ? CodeResult.SUCCESS : CodeResult.PARAMS_IS_INVALID,
                date = result.Item1
            };
        }
    }
}
